package com.lin.linchatgpt.service.impl;

import com.lin.linchatgpt.entity.Subject;
import com.lin.linchatgpt.entity.table.MessageTableDef;
import com.lin.linchatgpt.entity.table.SubjectTableDef;
import com.lin.linchatgpt.mapper.SubjectMapper;
import com.lin.linchatgpt.service.MessageService;
import com.lin.linchatgpt.service.SubjectService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Copyright(C),2022年-2022,霖霖
 *
 * @author 霖霖
 * @version 1.0
 * @date 2023/6/13 19:48
 * @Description 聊天记录历史service
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Resource
    private MessageService messageService;

    @Override
    public List<Subject> getSubjects(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(SubjectTableDef.SUBJECT.USER_ID.eq(userId));
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAndMessageById(String subjectId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(MessageTableDef.MESSAGE.SUBJECT_ID.eq(subjectId));
        return messageService.remove(queryWrapper) && this.removeById(subjectId);
    }
}
